package com.sakura.mall.coupon.homeSubject.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.coupon.homeSubject.domain.entity.HomeSubjectEntity;
import com.sakura.mall.coupon.homeSubject.domain.request.AddHomeSubjectRequest;
import com.sakura.mall.coupon.homeSubject.domain.request.UpdateHomeSubjectRequest;
import com.sakura.mall.coupon.homeSubject.domain.request.HomeSubjectPageRequest;
import com.sakura.mall.coupon.homeSubject.domain.vo.HomeSubjectVo;
import com.sakura.mall.coupon.homeSubject.mapper.HomeSubjectMapper;
import com.sakura.mall.coupon.homeSubject.service.HomeSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 首页专题表【jd首页下面很多专题，每个专题链接新的页面，展示专题商品信息】 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class HomeSubjectServiceImpl extends ServiceImpl<HomeSubjectMapper, HomeSubjectEntity> implements HomeSubjectService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddHomeSubjectRequest request) {
        // todo 其他校验逻辑
        HomeSubjectEntity homeSubject = request.convert(HomeSubjectEntity.class);
        this.save(homeSubject);
    }

    @Override
    public Page<HomeSubjectVo> queryPage(HomeSubjectPageRequest request) {
        IPage<HomeSubjectEntity> qp = request.toPage();
        LambdaQueryWrapper<HomeSubjectEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<HomeSubjectVo> ret = qp.convert(entity -> entity.convert(HomeSubjectVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateHomeSubjectRequest request) {
        HomeSubjectEntity entity = request.convert(HomeSubjectEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

